package controller;


import com.google.gson.Gson;
import dao.OrderDao;
import dao.impl.OrderDaoImpl;
import domain.User;
import domain.UserScore;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HLF
 * @version V1.0
 * @package controller
 * @date 2021/10/30 18:52
 */
public class UserController extends BaseController {
    private UserService service = new UserServiceImpl();
    private OrderDao orderDao = new OrderDaoImpl();

    /**
     * 登录方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String password = req.getParameter("password");
        User user2 = service.loginUser(new User(id, password));
        UserScore userScore = orderDao.selectUserScope(id);
        if (user2 != null) {
            //把用户信息保存在session中，便于显示已登录的用户信息
            req.getSession().setAttribute("userScore",userScore);
            req.getSession().setAttribute("user", user2);
            resp.sendRedirect("/HfsSale/index.jsp");
        } else {
            req.getRequestDispatcher("/pages/user/login.jsp?login=0").forward(req, resp);//0表示登录失败
        }
    }

    /**
     * ajax请求过来，判断用户是否存在
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void existsUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        User user = service.existsUser(id);
        Boolean existsUser = false;
        if (user != null) {
            existsUser = true;
        }
        Map<String, Boolean> map = new HashMap<>();
        map.put("existsUser", existsUser);

        Gson gson = new Gson();
        String jsonString = gson.toJson(map);

        resp.getWriter().write(jsonString);
    }

    /**
     * 注册
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        int i = service.registUser(new User(id, name, password));
        if (i > 0) {
            req.getRequestDispatcher("/pages/user/registSuccess.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("/pages/user/regist.jsp?regist=0").forward(req, resp);
        }
    }

    /**
     * 退出登录
     */
    protected void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("user");
        resp.sendRedirect("/HfsSale/index.jsp");
    }

    /**
     * 修改个人信息
     */
    protected void updateUserMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        User user = new User(id, name, password);
        int i = service.updateUser(user);
        if (i > 0) {
            req.getRequestDispatcher("/user/userController?action=login&i=" + i).forward(req, resp);
        }
    }

    /**
     * 显示所有用户
     */
    protected void showAllUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> allUsers = service.selectAllUsers();
        int d = WebUtils.parseInt(req.getParameter("d"), 0);
        req.getSession().setAttribute("allUsers", allUsers);
        resp.sendRedirect("/HfsSale/pages/admin/newUserAudit.jsp?d="+d);
    }

    /**
     * 删除违法用户
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        int d = service.deleteUser(id);
        resp.sendRedirect("/HfsSale/user/userController?action=showAllUsers&d=" + d);

    }
}
